import * as THREE from 'three';
import Stats from 'stats.js';
export default class Application {
    constructor({
        dom, scene, defaultWidth, defaultHeight,
        rendererOptions
    }) {
        this.dom = dom;
        this.scene = scene;
        this.rendererOptions = rendererOptions;
        this.defaultWidth = defaultWidth;
        this.defaultHeight = defaultHeight;
        this.scene.dom = this.dom;
        this.loaded = false;
        this.destroyed = false;
        this.storeDeltaTime = 1000;
        this.animationFrame = null;
        this.lastStoreTime = 0;
        console.log(2);
        if (this.dom) {
            this.dom.style.position = 'relative';
        }
        this.start();
    }

    update() {
        this.scene.Update();
    }

    start() {
        this.canvas = document.createElementNS('http://www.w3.org/1999/xhtml', 'canvas');
        this.canvas.style.display = 'block';
        const width = this.dom.clientWidth || this.defaultWidth || window.innerWidth;
        const height = this.dom.clientHeight || this.defaultHeight || window.innerHeight;
        this.canvas.width = width;
        this.canvas.height = height;
        this.dom.appendChild(this.canvas);
        this.renderer = new THREE.WebGLRenderer({
            canvas: this.canvas,
            antialias: true,
            logarithmicDepthBuffer: false,
            powerPreference: 'high-performance',
            ...this.rendererOptions
        });
        this.renderer.localClippingEnabled = true;
        const pixelRadio = Math.min(2, window.devicePixelRatio);
        this.renderer.setPixelRatio(pixelRadio);
        this.renderer.setSize(width, height);

        this.renderer.shadowMap.enabled = true;
        this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
        this.scene.Init(this.renderer, this);
        this.loaded = true;
        this.renderer.setAnimationLoop(() => { this.update(); });
        // 挂在应用级事件
        this.mountApplationEvent();
    }

    // 挂载应用级事件
    mountApplationEvent() {
        let lostContext = false;
        this.__webglcontextlost_fun_ = () => {
            lostContext = true;
        };

        // webgl 上下文丢失事件
        this.canvas.addEventListener('webglcontextlost', this.__webglcontextlost_fun_);

        // webgl 上下文丢失后 再切回该页面 刷新页面
        this.__window__visibilitychange__ = () => {
            if (lostContext) {
                window.location.reload();
            }
        };

        document.addEventListener('visibilitychange', this.__window__visibilitychange__);
    }

    unMountApplationEvent() {
        this.canvas.removeEventListener('webglcontextlost', this.__webglcontextlost_fun_);
        document.removeEventListener('visibilitychange', this.__window__visibilitychange__);
    }

    resize(width, height) {
        if (!this.loaded) {
            return;
        }
        const nwidth = width || this.dom.offsetWidth || this.defaultWidth;
        const nheight = height || this.dom.clientHeight || this.defaultHeight;
        this.canvas.width = nwidth;
        this.canvas.height = nheight;
        if (this.renderer) {
            this.renderer.setSize(nwidth, nheight);
        }

        this.scene.resize();
    }

    // 移除dom
    destroy() {
        this.unMountApplationEvent();
        this.destroyed = true;
        this.scene.Destroy();

        this.renderer.renderLists.dispose();
        this.renderer.dispose();
        if (this.renderer.domElement && this.renderer.domElement.parentElement) {
            this.renderer.domElement.parentElement.removeChild(this.renderer.domElement);
        }
        this.renderer = null;
    }
}
